package com.fyp.ptma.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.fyp.ptma.dao.service.iservice.IApplicationService;
import com.fyp.ptma.dao.service.iservice.IGroupService;
import com.fyp.ptma.dao.service.iservice.IUserService;
import com.fyp.ptma.model.Application;
import com.fyp.ptma.model.ApplicationVersion;
import com.fyp.ptma.model.Downloader;
import com.fyp.ptma.model.Group;
import com.fyp.ptma.property.PropertyFile;

@Controller
@SessionAttributes({ "applications" })
@RequestMapping(value = "/secure")
public class SecureLinkNavigator extends AbstractController {

	@Autowired
	private IUserService userService;

	@Autowired
	private IApplicationService appService;
	@Autowired
	IGroupService groupService;

	// Applications Page

	@RequestMapping(value = "/applications.html", method = RequestMethod.GET)
	public ModelAndView secureShowUserApplications(Model model) {

		model.addAttribute("pageTitle",
				PropertyFile.valueOf("user.applications"));

		model.addAttribute("appName",
				PropertyFile.valueOf("app.name.camelcase"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String email = auth.getName();

		if (null != email) {
			List<Application> applications = userService
					.getAllApplications(userService.getUserByEmail(email));
			model.addAttribute("applications", applications);
			model.addAttribute("userName", email);
		}

		return new ModelAndView("secure/applications");
	}

	// File Upload Page
	@RequestMapping(value = "/fileUpload.html", method = RequestMethod.GET)
	public String secureDisplayUploadPage(Model model) {
		model.addAttribute("pageTitle", PropertyFile.valueOf("upload.build"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName(); // get logged in username

		model.addAttribute("username", name);
		return "secure/upload";
	}

	// Application Detail

	@RequestMapping(value = "/appDetail.html", method = RequestMethod.GET)
	public String secureApplicationDetail(Model model,
			HttpServletRequest request) {
		model.addAttribute("pageTitle", PropertyFile.valueOf("title.appDetail"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		request.getSession().getAttribute("applications");
		String paramPkg = request.getParameter("pkg");
		if (null != paramPkg) {
			Application app = ((List<Application>) request.getSession()
					.getAttribute("applications")).get(Integer
					.parseInt(paramPkg));

			List<ApplicationVersion> appVersion = appService
					.getAllApplicationVersions(app);
			model.addAttribute("ApplicationName", app.getApplicationName());
			model.addAttribute("appVersions", appVersion);
		}
		return "secure/applicationDetail";
	}

	// Invite User

	@RequestMapping(value = "/addDownloader.html", method = RequestMethod.GET)
	public String secureAddTester(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		model.addAttribute("pageTitle", PropertyFile.valueOf("title.appDetail"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		String name = SecurityContextHolder.getContext().getAuthentication()
				.getName();

		return "secure/addTester";
	}

	// Delete Application
	@RequestMapping(value = "/deleteApplication.html", method = RequestMethod.GET)
	public String secureDeleteApplication(Model model,
			HttpServletRequest request) {
		model.addAttribute("pageTitle", PropertyFile.valueOf("title.appDetail"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		String paramPkg = request.getParameter("pkg");
		if (null != paramPkg) {
			@SuppressWarnings("unchecked")
			Application app = ((List<Application>) request.getSession()
					.getAttribute("applications")).get(Integer
					.parseInt(paramPkg));

			appService.delete(app);
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			String email = auth.getName();

			if (null != email) {
				List<Application> applications = userService
						.getAllApplications(userService.getUserByEmail(email));

				model.addAttribute("applications", applications);
				model.addAttribute("userName", email);
			}
		}
		return "secure/applications";
	}

	// Groups

	@RequestMapping(value = "/devices.html", method = RequestMethod.GET)
	public String showDevices(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		model.addAttribute(
				"pageTitle", PropertyFile.valueOf("title.devices"));
		model.addAttribute(
				"ptma",
				PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute(
				"slogon", PropertyFile.valueOf("app.slogon"));

		String name = SecurityContextHolder.getContext().getAuthentication()
				.getName();
		Application app = (Application)  request.getSession().getAttribute("application");
		app.setNameSpace("com.ftv.gk");
		if (app != null && null != name) {
			List<Downloader> downloaders = appService.getAllDevicesFor(app);
			model.addAttribute(
					"devices", downloaders);
			 
		}

		return "secure/devices";
	}

	@RequestMapping(value = "/groups.html", method = RequestMethod.GET)
	public String secureGroups(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		model.addAttribute("pageTitle", PropertyFile.valueOf("title.groups"));
		model.addAttribute("ptma", PropertyFile.valueOf("app.name.abbrivation"));
		model.addAttribute("slogon", PropertyFile.valueOf("app.slogon"));

		String name = SecurityContextHolder.getContext().getAuthentication()
				.getName();
		List<Group> groups = groupService.getAllUserGroups(name);
		model.addAttribute("groups", groups);
		return "secure/groups";
	}
}
